package com.punch.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punch.common.dto.system.SelectCourseDTO;
import com.punch.common.exception.BusinessException;
import com.punch.common.page.PageInfo;
import com.punch.common.query.syste.SelectCourseQueryInfo;
import com.punch.common.simple.redis.utils.UserUtil;
import com.punch.common.tools.BasePage;
import com.punch.common.utils.ImageUtil;
import com.punch.common.vo.system.SelectCourseVO;
import com.punch.system.exception.SysExceptionEnum;
import com.punch.system.mapper.SelectCourseMapper;
import com.punch.system.model.SelectCourse;
import com.punch.system.model.SysNationArea;
import com.punch.system.service.NationAreaService;
import com.punch.system.service.SelectCourseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 精选课程表 服务实现类
 * </p>
 *
 * @author wjj
 * @since 2020-12-14
 */
@Service
public class SelectCourseServiceImpl extends ServiceImpl<SelectCourseMapper, SelectCourse> implements SelectCourseService {

    @Resource
    private NationAreaService nationAreaService;

    /**
     * 精选课程列表
     *
     * @param queryInfo 查询条件
     * @return PageInfo<SelectCourseVO>
     */
    @Override
    public PageInfo<SelectCourseVO> listSelectCourse(SelectCourseQueryInfo queryInfo) {
        queryInfo.setCurrentAreaId(UserUtil.getLoginUser().getAreaId());
        queryInfo.setSortStr("sort_by:asc");
        return BasePage.queryBasePage(queryInfo, params -> this.getBaseMapper().queryCount(params),
                params -> {
                    List<SelectCourseVO> retList = getBaseMapper().queryList(params);
                    retList.forEach(selectCourseVO -> {
                        if (selectCourseVO.getStatus() == 0) {
                            selectCourseVO.setUpdateTime(null);
                        }
                    });
                    return retList;
                });
    }

    /**
     * 保存精选课程
     *
     * @param selectCourseDTO 提交表单
     * @return id
     */
    @Override
    public Integer saveSelectCourse(SelectCourseDTO selectCourseDTO) {
        SelectCourse selectCourse = new SelectCourse();
        BeanUtil.copyProperties(selectCourseDTO, selectCourse);
        //设置所有地区
        Integer areaId = selectCourseDTO.getAreaId();
        SysNationArea area = nationAreaService.getById(areaId);
        selectCourse.setAllAreaIds(area.getParentIds() + "," + areaId);
        Long userId = UserUtil.getLoginUserId();
        selectCourse.setCreateId(userId);
        //处理精选图片前缀
        if (StrUtil.isNotEmpty(selectCourseDTO.getCourseImg())) {
            String img = ImageUtil.removePrefix(selectCourseDTO.getCourseImg());
            selectCourse.setCourseImg(img);
        }
        if (!save(selectCourse)) {
            throw new BusinessException(SysExceptionEnum.DATA_INSERT_EXCEPTION);
        }
        return selectCourse.getId();
    }

    /**
     * 修改精选课程
     *
     * @param selectCourseDTO 提交表单
     * @return id
     */
    @Override
    public Integer updateSelectCourse(SelectCourseDTO selectCourseDTO) {
        SelectCourse selectCourse = new SelectCourse();
        BeanUtil.copyProperties(selectCourseDTO, selectCourse);
        selectCourse.setId(selectCourseDTO.getSelectCourseId());
        //处理精选课程图片前缀
        if (StrUtil.isNotEmpty(selectCourseDTO.getCourseImg())) {
            String img = ImageUtil.removePrefix(selectCourseDTO.getCourseImg());
            selectCourse.setCourseImg(img);
        } else {
            selectCourse.setCourseImg("");
        }
        if (!updateById(selectCourse)) {
            throw new BusinessException(SysExceptionEnum.DATA_UPDATE_EXCEPTION);
        }
        return selectCourse.getId();
    }

    /**
     * 精选课程详情
     *
     * @param selectCourseId selectCourseId
     * @return SelectCourseVO
     */
    @Override
    public SelectCourseVO getSelectCourse(Integer selectCourseId) {
        SelectCourse selectCourseEX = getById(selectCourseId);
        Optional.ofNullable(selectCourseEX).orElseThrow(() -> new BusinessException(SysExceptionEnum.DATA_NOT_EXIST));
        SelectCourseVO selectCourseVO = new SelectCourseVO();
        BeanUtil.copyProperties(selectCourseEX, selectCourseVO);
        //处理活动图片前缀
        if (StrUtil.isNotEmpty(selectCourseEX.getCourseImg())) {
            String img = ImageUtil.addPrefix(selectCourseEX.getCourseImg());
            selectCourseVO.setCourseImg(img);
        }
        selectCourseVO.setSelectCourseId(selectCourseId);
        return selectCourseVO;
    }

    /**
     * 删除精选课程
     *
     * @param selectCourseId selectCourseId
     * @return id
     */
    @Override
    public Integer deleteSelectCourse(Integer selectCourseId) {
        SelectCourse selectCourseEX = getById(selectCourseId);
        Optional.ofNullable(selectCourseEX).orElseThrow(() -> new BusinessException(SysExceptionEnum.DATA_NOT_EXIST));
        //已经展示的不能删除,需要下架
        if (selectCourseEX.getStatus() == 1) {
            throw new BusinessException(SysExceptionEnum.STATUS_ERROR);
        }
        if (!removeById(selectCourseId)) {
            throw new BusinessException(SysExceptionEnum.DATA_DELETION_EXCEPTION);
        }
        return selectCourseId;
    }

    /**
     * 修改精选课程状态
     *
     * @param selectCourseId selectCourseId
     * @param status         状态 0-不显示 1-显示
     * @return selectCourseId
     */
    @Override
    public Integer updateSelectCourseStatus(Integer selectCourseId, Integer status) {
        SelectCourse selectCourseEX = getById(selectCourseId);
        Optional.ofNullable(selectCourseEX).orElseThrow(() -> new BusinessException(SysExceptionEnum.DATA_NOT_EXIST));
        if (selectCourseEX.getStatus().equals(status)) {
            throw new BusinessException(SysExceptionEnum.STATUS_ERROR);
        }
        SelectCourse selectCourse = new SelectCourse();
        selectCourse.setStatus(status);
        selectCourse.setId(selectCourseId);
        if (!updateById(selectCourse)) {
            throw new BusinessException(SysExceptionEnum.DATA_UPDATE_EXCEPTION);
        }
        return selectCourseId;
    }
}
